﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using QLZJFurnitureSCM.Model;
using QLZJFurnitureSCM.BLL;

namespace QLZJFurnitureSCM.WinForm
{
    public partial class FrmPurchase : Form
    {
        private ProductService productService = null;
        private FurnitureTypeService furnitureTypeService = null;
        private PurchaseService purchaseService = null;
        private List<Product> lstProduct = null;

        public FrmPurchase()
        {
            InitializeComponent();
            productService = new ProductService();
            furnitureTypeService = new FurnitureTypeService();
        }

        private void btnPurchaseAdd_Click(object sender, EventArgs e)
        {
            Product product = new Product();
            Purchase purchase = new Purchase();
            string status = string.Empty;
            bool isNew = rdoNew.Checked;
            bool canInsert = false;
            if (isNew)
            {
                string productName = cboProducts.Text;
                if ((from p in lstProduct where p.ProductName == productName select p).Count() > 0)
                {
                    this.toolStripStatusOP.Text = "该产品已经存在, 请修改再添加.";
                    this.cboProducts.Focus();
                    return;
                }
            }
            /*
            else
            {
                Product temp = ((Product)cboProducts.SelectedItem);
                product.ProductName = temp.ProductName;
                product.ProductId = temp.ProductId;
            }*/
            int count = (int)this.numericUpDownCount.Value;
            FurnitureType furniture = ((FurnitureType)cboType.SelectedItem);
            if(null == furniture)
            {
                this.toolStripStatusOP.Text = "请先添加类别.";
                return;
            }
            product.ProductType = furniture.TypeId;
            product.ProductName = cboProducts.Text;
            /*
             //上面已用linq实现
            if (!productService.CheckExistProductsByProductName(product))
            {
                this.toolStripStatusOP.Text = "此产品已存在于当前分类, 请修改重试.";
                return;
            }*/
            product.ProductDimension = this.txtDimension.Text;
            product.ProductPricePurchase = decimal.Parse(this.txtPricePurchase.Text);
            product.ProductPriceSale = decimal.Parse(this.txtPriceSale.Text);
            product.ProductCountAccumulate = count;
            product.ProductCountSale = 0;
            product.ProductDescription = this.txtDescription.Text;

            purchase.PurchaseCount = count;
            purchase.PurchaseDate = DateTime.Now;
            purchase.PurchasePrice = product.ProductPricePurchase;
            purchase.PurchaseProductType = furniture.TypeName;
            purchase.PurchaseProductName = product.ProductName;
            purchase.PurchaseByUser = FrmMain.user.NickName;
            purchase.PurchaseTotalPrice = purchase.PurchaseCount * purchase.PurchasePrice;

            purchaseService = new PurchaseService();
            if(isNew)
            {
                product = productService.InsertProduct(product);
                if (null != product)
                {
                    if (product.ProductId > 0)
                    {
                        canInsert = true;
                        lstProduct.Add(product);
                    }
                }
            }else
            {
                int purchaseCount = int.Parse(this.txtAccumulate.Text);
                Product p = (Product)cboProducts.SelectedItem;
                //decimal pPrice = p.ProductPrice;
                product.ProductId = p.ProductId;
                //product.ProductCountAccumulate = p.ProductCountAccumulate;
                product.ProductCountAccumulate += purchaseCount;
                if (productService.UpdateProductPriceCount(product))
                {
                    canInsert = true;
                    if (product.ProductPricePurchase != p.ProductPricePurchase)
                    {
                        //添加价格修改记录
                        ProductPriceChange productPriceChange = new ProductPriceChange();
                        productPriceChange.ProductId = product.ProductId;
                        productPriceChange.ProductName = product.ProductName;
                        productPriceChange.ProductPrice = p.ProductPricePurchase;
                        productPriceChange.ProductPriceNow = product.ProductPricePurchase;
                        productPriceChange.ProductChangeBy = FrmMain.user.NickName;
                        productPriceChange.ProductChangeDate = DateTime.Now;
                        ProductPriceChangeService productPriceChangeService = new ProductPriceChangeService();
                        productPriceChange = productPriceChangeService.InsertProductPriceChange(productPriceChange);
                        if (productPriceChange.ChangeId > 0)
                            status = "添加成功.";
                        else
                            status = "添加失败.";
                    }
                    //进货表添加记录
                    /*
                    purchase = purchaseService.InsertPurchase(purchase);
                    if (purchase.PurchaseId > 0)
                    {
                        MessageBox.Show("添加成功.";)
                        return;
                    } */
                }
            }
            //进货表添加记录

            if (canInsert)
            {
                purchase = purchaseService.InsertPurchase(purchase);
                if (purchase.PurchaseId > 0)
                {
                    status = "添加成功.";
                    if (!chkPurchaseContinue.Checked)
                        this.DialogResult = DialogResult.OK;
                    else
                        this.txtAccumulate.Text = product.ProductCountAccumulate.ToString();
                    //return;
                }
            }
            else
                status = "添加失败.";
            //可以进行font处理.
            this.toolStripStatusOP.Text = status;
            //this.DialogResult = DialogResult.None;
            //进货记录
            
        }

        private void FrmPurchase_Load(object sender, EventArgs e)
        {
            InitProducts();
            InitTypes();
        }

        private void InitTypes()
        {
            cboType.Items.AddRange(furnitureTypeService.ReadAll().ToArray());
            cboType.DisplayMember = "TypeName";
            cboType.ValueMember = "TypeId";
            if(cboType.Items.Count > 0)
                cboType.SelectedIndex = 0;
        }

        private void InitProducts()
        {
            lstProduct = productService.ReadAll();
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //get Products by typeId;
            cboProducts.Items.Clear();
            txtDimension.Clear();
            txtPricePurchase.Clear();
            txtDescription.Clear();
            txtAccumulate.Clear();
            //this can build a method in service.
            List<Product> lstProductByType = (from product in lstProduct where product.ProductType == ((FurnitureType)cboType.SelectedItem).TypeId select product).ToList();
            if (lstProductByType.Count > 0)
            {
                cboProducts.Items.AddRange(lstProductByType.ToArray());
                cboProducts.DisplayMember = "ProductName";
                cboProducts.ValueMember = "ProductId";
                cboProducts.SelectedIndex = 0;
            }
        }

        private void cboProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            //change the price;

            if (null != cboProducts.SelectedItem)
            {
                Product product = ((Product)cboProducts.SelectedItem);
                if (product != null)
                {
                    txtDimension.Text = product.ProductDimension;
                    txtPricePurchase.Text = product.ProductPricePurchase.ToString();
                    txtDescription.Text = product.ProductDescription;
                    txtAccumulate.Text = product.ProductCountAccumulate.ToString();
                    txtPriceSale.Text = product.ProductPriceSale.ToString();
                }
            }
        }

        private void rdoExist_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoExist.Checked)
            {
                cboType_SelectedIndexChanged(sender, e);
                this.cboProducts.DropDownStyle = ComboBoxStyle.DropDownList;
            }
            else
            {
                cboProducts.Focus();
                this.cboProducts.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
            else if (Char.IsPunctuation(e.KeyChar))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.LastIndexOf('.') != -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
        }

    }
}
